home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CTCPResponder.cp < prev    next >
Encoding:
Text File  |  1998-08-10  |  6.4 KB  |  234 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CTCPResponder.cp            ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    Responders represent the endpoints that handle the actual connection to and
  5. //    interaction with remote clients.
  6.  
  7. #include "CTCPResponder.h"
  8. #include "CTCPResponderThread.h"
  9.  
  10. #include <UNetworkFactory.h>
  11.  
  12. #include <LWindow.h>
  13. #include <LApplication.h>
  14.  
  15. #include <Sound.h>
  16.  
  17.  
  18. // ===========================================================================
  19. //        • Resource IDs
  20. // ===========================================================================
  21.  
  22. const PP_PowerPlant::ResIDT    PPob_TCPTerminalWindow    = 129;
  23. const PP_PowerPlant::PaneIDT    pTerminal                = 'TERM';
  24.  
  25.  
  26. // ===========================================================================
  27.  
  28. #pragma mark ••• CTCPResponder •••
  29.  
  30.  
  31. // ---------------------------------------------------------------------------
  32. //        • CTCPResponder
  33. // ---------------------------------------------------------------------------
  34.  
  35. CTCPResponder::CTCPResponder(
  36.     LCommander* inSuper)
  37.         : LSingleDoc(inSuper)
  38. {
  39.     mTerminalPane = nil;
  40.     mEndpoint = nil;
  41.     mLineBufferIndex = 0;
  42.     mLineMode = true;
  43.     mEchoCharacters = true;
  44.     mResponderThread = nil;
  45.     mQuitWhenDone = false;
  46.     mSaveOption = 0;
  47. }
  48.  
  49.  
  50. // ---------------------------------------------------------------------------
  51. //        • ~CTCPResponder
  52. // ---------------------------------------------------------------------------
  53.  
  54. CTCPResponder::~CTCPResponder()
  55. {
  56.     if (mEndpoint) {
  57.         delete mEndpoint;
  58.         mEndpoint = nil;
  59.     }
  60. }
  61.  
  62. // ---------------------------------------------------------------------------
  63. //        • Accept
  64. // ---------------------------------------------------------------------------
  65. //    Accept a connection from a remote machine on this endpoint. This endpoint
  66. //    will do all the dirty work. 
  67. //    The display window is also opened at this time.
  68. //
  69. //    This routine creates an endpoint, but does NOT bind it!
  70. //    The endpoint is automagically bound to the port on which incomming connection
  71. //    was received via the 'AcceptIncoming' routine inside the thread.
  72.  
  73. void
  74. CTCPResponder::Accept(CSimpleTCPServer * inServer)
  75. {
  76.     BuildServerWindow();
  77.  
  78.     mEndpoint = PP_PowerPlant::UNetworkFactory::CreateTCPEndpoint();
  79.  
  80.     mResponderThread = new CTCPResponderThread(mEndpoint, mTerminalPane, this, inServer);
  81.     mResponderThread->Resume();
  82. }
  83.  
  84. // ---------------------------------------------------------------------------
  85. //        • IsIdle
  86. // ---------------------------------------------------------------------------
  87.  
  88. Boolean
  89. CTCPResponder::IsIdle()
  90. {
  91.     if (mEndpoint == nil)
  92.         return true;
  93.     PP_PowerPlant::EEndpointState netState = mEndpoint->GetState();
  94.     return ((netState == PP_PowerPlant::endpoint_Idle) || (netState == PP_PowerPlant::endpoint_Unbound));
  95. }
  96.  
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        • ServerThreadDied
  100. // ---------------------------------------------------------------------------
  101.  
  102. void
  103. CTCPResponder::ServerThreadDied()
  104. {
  105.     mResponderThread = nil;
  106.  
  107.     //Re-issue the quit command when we are ready.
  108.     if (mQuitWhenDone) {
  109.         PP_PowerPlant::LApplication* theApp = dynamic_cast<PP_PowerPlant::LApplication*>(GetSuperCommander());
  110.         if (theApp) {
  111.             theApp->DoQuit(mSaveOption);
  112.         }
  113.     }
  114.  
  115.     delete this;
  116. }
  117.  
  118. // ---------------------------------------------------------------------------
  119. //        • Disconnect
  120. // ---------------------------------------------------------------------------
  121.  
  122. void
  123. CTCPResponder::Disconnect()
  124. {
  125.     if (mResponderThread) {
  126.         mResponderThread->StartDisconnect();
  127.     }
  128. }
  129.  
  130. // ---------------------------------------------------------------------------
  131. //        • BindCompleted
  132. // ---------------------------------------------------------------------------
  133.  
  134. void
  135. CTCPResponder::BindCompleted()
  136. {
  137.     PP_PowerPlant::LInternetAddress* theAddress = mEndpoint->GetRemoteHostAddress();
  138.     SetWindowTitle(*theAddress);
  139.     delete theAddress;
  140. }
  141.  
  142. // ---------------------------------------------------------------------------
  143. //        • BindFailed
  144. // ---------------------------------------------------------------------------
  145.  
  146. void
  147. CTCPResponder::BindFailed()
  148. {
  149. }
  150.  
  151. // ---------------------------------------------------------------------------
  152. //        • HandleKeyPress
  153. // ---------------------------------------------------------------------------
  154. //    Server side does not allow user interaction with the server window...
  155. //    We simply beep and swallow the event.
  156.  
  157. Boolean
  158. CTCPResponder::HandleKeyPress(
  159.     const EventRecord& /* inKeyEvent */)
  160. {
  161.     ::SysBeep(30);
  162.     return true;
  163. }
  164.  
  165. // ---------------------------------------------------------------------------
  166. //        • BuildSessionWindow
  167. // ---------------------------------------------------------------------------
  168. //    Build the window for this connection session.
  169.  
  170. void
  171. CTCPResponder::BuildServerWindow()
  172. {
  173.     mWindow = PP_PowerPlant::LWindow::CreateWindow(PPob_TCPTerminalWindow, this);
  174.     mTerminalPane = dynamic_cast <CTerminalPane*> (mWindow->FindPaneByID(pTerminal));
  175.     ThrowIfNil_ (mTerminalPane);
  176. }
  177.  
  178. // ---------------------------------------------------------------------------
  179. //        • SetWindowTitle
  180. // ---------------------------------------------------------------------------
  181.  
  182. void
  183. CTCPResponder::SetWindowTitle(
  184.     PP_PowerPlant::LInternetAddress & inAddress)
  185. {
  186.     if (mWindow) {
  187.         Str255 titleStr;
  188.         inAddress.GetIPDescriptor(titleStr);
  189.         mWindow->SetDescriptor(titleStr);
  190.     }
  191. }
  192.  
  193. // ---------------------------------------------------------------------------
  194. //        • AllowSubRemoval
  195. // ---------------------------------------------------------------------------
  196. //    If user tries to close the connection window, interpret this as a request
  197. //    to close the connection. The window is not immediately closed; rather
  198. //    it is closed when the connection actually goes away.
  199.  
  200. Boolean
  201. CTCPResponder::AllowSubRemoval(
  202.     LCommander *inSub)
  203. {
  204.     if ((inSub == mWindow) && !IsIdle()) {
  205.         Disconnect();
  206.         return false;
  207.     }
  208.     else
  209.         return LSingleDoc::AllowSubRemoval(inSub);
  210. }
  211.  
  212. // ---------------------------------------------------------------------------
  213. //        • AttemptQuitSelf
  214. // ---------------------------------------------------------------------------
  215. //    Close our connection before quitting. We re-issue the quit command via
  216. //    the ServerThreadDied method once we are done.
  217.  
  218. Boolean
  219. CTCPResponder::AttemptQuitSelf(
  220.     SInt32    inSaveOption)
  221. {
  222.     if (mQuitWhenDone) {
  223.         return LCommander::AttemptQuitSelf(inSaveOption);
  224.     } else {
  225.         mQuitWhenDone = true;
  226.         mSaveOption = inSaveOption;
  227.  
  228.         Disconnect();
  229.         return false;
  230.     }
  231. }
  232.  
  233.  
  234.